package android.pot.com.smartcar.ui.my.activity;

import android.os.Bundle;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.action.Action;
import android.pot.com.smartcar.base.CommonRecycleAdapter;
import android.pot.com.smartcar.base.MyResponseHandler;
import android.pot.com.smartcar.base.RecycleViewHolder;
import android.pot.com.smartcar.base.swipe.SwipeBackActivity;
import android.pot.com.smartcar.entity.CollectionEntity;
import android.pot.com.smartcar.entity.UserInfo;
import android.pot.com.smartcar.tool.LoadingTool;
import android.pot.com.smartcar.ui.home.activity.ShopDetailActivity;
import android.pot.com.smartcar.util.GsonUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaoshilu on 2017/11/27.
 */
public class MyCollectionActivity extends SwipeBackActivity {

    private RefreshLayout refreshLayout;

    private RecyclerView mRecyclerView;

    private View ll_empty_view;

    private CommonRecycleAdapter mAdapter;

    private int curPage = 1;

    private int pageCount;

    private boolean showLoading = true;

    private List<CollectionEntity.CollectionData> mDatas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        initView();
        addListener();
        initData();
    }

    @Override
    protected void initView() {
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("我的收藏");
        refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        ll_empty_view = findViewById(R.id.ll_empty_view);
        initListView();
    }

    private void initListView(){
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecycleAdapter<CollectionEntity.CollectionData>(this, R.layout.listcell_home_car, mDatas) {
            @Override
            public void convert(RecycleViewHolder helper, final CollectionEntity.CollectionData item) {
                if (helper.getPosition() == 0){
                    helper.setVisible(R.id.top_view,true);
                }else{
                    helper.setVisible(R.id.top_view,false);
                }
                helper.setImageByUrl(R.id.iv_icon,item.getPlan());
                helper.setText(R.id.tv_title,item.getShop_name());
                helper.setText(R.id.tv_site,item.getShop_address());
                helper.setText(R.id.tv_distance,item.getDistance());
                if (item.getCertification_status() == 1){
                    helper.setVisible(R.id.tv_identify,true);
                }else{
                    helper.setVisible(R.id.tv_identify,false);
                }
                helper.setOnClickListener(R.id.rl_outer, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Bundle bundle = new Bundle();
                        bundle.putString("shop_id",item.getId());
                        startAct(MyCollectionActivity.this, ShopDetailActivity.class,bundle);
                    }
                });


            }
        };

        mRecyclerView.setAdapter(mAdapter);
    }


    @Override
    protected void addListener() {
        findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollToFinishActivity();
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mDatas.clear();
                curPage = 1;
                showLoading = false;
                getDetailData();

            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                curPage++;
                showLoading = false;
                getDetailData();
            }
        });
    }

    @Override
    protected void initData() {

        getDetailData();
    }


    private void getDetailData(){
        Map<String,String> params = new HashMap<>();
        params.put("lat",UserInfo.instance().getLat());
        params.put("lng", UserInfo.instance().getLng());
        params.put("page",curPage+"");
        params.put("limit","10");
        if (showLoading){
            LoadingTool.StartLoading(this);
        }

        Action.get().collection_list(this, params, new MyResponseHandler() {
            @Override
            public void onSuccess(String message,JSONObject response) {
                if (showLoading) {
                    LoadingTool.EndLoading(MyCollectionActivity.this);
                }
                try {
                    CollectionEntity orderEntity = GsonUtil.getObject(response.toString(), CollectionEntity.class);
                    pageCount = orderEntity.getPage().getPage_count();
                    mDatas.addAll(orderEntity.getData());
                }catch (Exception e){

                }
                if (mDatas.size()>0){
                    ll_empty_view.setVisibility(View.GONE);
                }else{
                    ll_empty_view.setVisibility(View.VISIBLE);
                }
                mAdapter.notifyDataSetChanged();
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
                if (mDatas.size() < pageCount) {
                    refreshLayout.setEnableLoadMore(true);
                } else {
                    refreshLayout.setEnableLoadMore(false);
                }
            }

            @Override
            public void onFailure(int statusCode, String error_msg) {
                super.onFailure(statusCode, error_msg);
                if (mDatas.size()>0){
                    ll_empty_view.setVisibility(View.GONE);
                }else{
                    ll_empty_view.setVisibility(View.VISIBLE);
                }
                mAdapter.notifyDataSetChanged();
                refreshLayout.setEnableLoadMore(false);
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
            }
        });
    }


}